    package com.zzh.utils;

    import lombok.extern.slf4j.Slf4j;
    import org.junit.platform.commons.util.StringUtils;

    import javax.servlet.http.HttpServletRequest;


    /**
     * @author wenrouyue
     */
    @Slf4j
    public class IpUtil {

        public static String getIpAddr(HttpServletRequest request) {
            String ip = request.getHeader("X-Real-IP");
            log.info("X-Real-IP:{}",ip);
            if (!StringUtils.isBlank(ip) && !"unknown".equalsIgnoreCase(ip)) {
                return ip;
            }
            ip = request.getHeader("X-Forwarded-For");
            log.info("X-Forwarded-For:{}",ip);
            if (!StringUtils.isBlank(ip) && !"unknown".equalsIgnoreCase(ip)) {
                // 多次反向代理后会有多个IP值，第一个为真实IP。
                int index = ip.indexOf(',');
                if (index != -1) {
                    return ip.substring(0, index);
                } else {
                    return ip;
                }
            } else {
                return request.getRemoteAddr();
            }
        }


    }

